import json
from typing import Dict
import os

from langchain import hub
from langchain.agents import create_tool_calling_agent, AgentExecutor
from langchain_community.tools import TavilySearchResults
from langchain_core.prompts import ChatPromptTemplate
from langchain_openai import ChatOpenAI
from langchain_community.chat_message_histories import ChatMessageHistory
from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
from langchain_core.runnables import RunnableWithMessageHistory
from langchain_core.messages import HumanMessage, BaseMessage
from langchain.chains.conversation.base import ConversationChain
from langchain.agents import AgentExecutor, create_tool_calling_agent,create_openai_tools_agent
from langgraph.prebuilt import chat_agent_executor
os.environ['LANGCHAIN_TRACING_V2'] = 'true'
os.environ['LANGCHAIN_PROJECT'] = 'LLMDEMO'
os.environ['LANGCHAIN_API_KEY'] = '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'
# 初始化带模板支持的LLM
llm = ChatOpenAI(
    api_key="sk-a3f7718fb81f43b2915f0a6483b6661b",
    base_url="https://dashscope.aliyuncs.com/compatible-mode/v1",
    model="qwen-plus",  # 此处以qwen-plus为例，您可按需更换模型名称。模型列表：https://help.aliyun.com/zh/model-studio/getting-started/models
    # other params...
)

prompt = hub.pull("hwchase17/openai-functions-agent")
print(prompt.messages)
search = TavilySearchResults(max_results =1)
tools = [search]
agent = create_openai_tools_agent(llm,tools,prompt)
agett_exe = AgentExecutor(agent=agent, tools=tools, verbose=True)
while True:
    input_text = input("请输入问题：")
    if input_text == "exit":
        break
    response = agett_exe.invoke({"input":input_text})

